package org.elucid.chess.piece;

import java.util.ArrayList;
import java.util.List;

import org.elucid.chess.model.Board.Direction;
import org.elucid.chess.model.Game;
import org.elucid.chess.model.Move;
import org.elucid.chess.model.Piece;
import org.elucid.chess.model.Square;

/**
 * 
 * @author george
 *
 */
public class King extends Piece {

	/**
	 * 
	 * @param isWhite
	 */
    public King(boolean isWhite) {
        super(isWhite);
    }

    @Override
    public List<Move> listLegalMoves(Game game, boolean isOnlyThreats) {
        List<Move> legalMoves = new ArrayList<Move>();

        Square fromSquare = game.getPieceSquares().get(this);
        // check one square in every direction
        for( Direction direction : Direction.values() )
        {
        	Square toSquare = game.getBoard().getSquareAt(fromSquare, direction, 1);
        	if (toSquare != null) {
    			Piece piece = game.getPieceSquares().getKey(toSquare);
    			if (piece == null || isOpponentsPiece(piece) )
    			{
    				if( !game.getThreatenedSquares().keySet().contains(toSquare) ) // King can't move into a threatened square
    				{
	    				Move move = new Move(fromSquare, toSquare, true );
	    				legalMoves.add(move);
    				}
    			}
        	}
        }
        
        return legalMoves;
    }

	
}
